﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BurnSystems.Gpx.Wpf
{
    /// <summary>
    /// Interaction logic for TrackSegment.xaml
    /// </summary>
    public partial class TrackSegment : UserControl
    {
        /// <summary>
        /// Stores the radius for the waypoints
        /// </summary>
        public const double waypointRadius = 0.0005;

        /// <summary>
        /// Stores a list of points for the path
        /// </summary>
        private List<Ellipse> points = new List<Ellipse>();

        /// <summary>
        /// Stores a list of points for the path
        /// </summary>
        private List<Line> lines = new List<Line>();

        /// <summary>
        /// Stores the tracksegment
        /// </summary>
        private GpxTrackSegment tracksegment;

        public TrackSegment()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the tracksegment
        /// </summary>
        public GpxTrackSegment Tracksegment
        {
            get { return this.tracksegment; }
            set
            {
                this.tracksegment = value;
                this.UpdateTrackSegment();
            }
        }

        /// <summary>
        /// Updates the tracksegment
        /// </summary>
        private void UpdateTrackSegment()
        {
            // Remove existing points
            foreach (var point in this.points)
            {
                this.Waypoints.Children.Remove(point);
            }

            // Skip if no track existing
            if (this.tracksegment == null)
            {
                return;
            }

            // Add points for track
            var waypoints = this.tracksegment.Waypoints;
            foreach (var waypoint in waypoints)
            {
                var ellipse = new Ellipse();
                ellipse.Width = waypointRadius;
                ellipse.Height = waypointRadius;
                ellipse.Fill = Brushes.Red;
                this.Waypoints.Children.Add(ellipse);
                Canvas.SetLeft(
                    ellipse, 
                    waypoint.Longitude - waypointRadius / 2);
                Canvas.SetTop(
                    ellipse, 
                    waypoint.Latitude - waypointRadius / 2);
            }

            for (var n = 0; n < waypoints.Count - 1; n++)
            {
                var line = new Line();
                line.X1 = waypoints[n].Longitude;
                line.Y1 = waypoints[n].Latitude;
                line.X2 = waypoints[n+1].Longitude;
                line.Y2 = waypoints[n+1].Latitude;
                line.StrokeThickness = waypointRadius / 4;
                line.Stroke = new SolidColorBrush(Colors.Yellow);

                this.Waypoints.Children.Add(line);
            }
        }

        /// <summary>
        /// Zooms to the waypoints
        /// </summary>
        public void ZoomIn()
        {            
            var bb = this.tracksegment.BoundingBox;

            var width = bb.Width + 2 * waypointRadius;
            var height = bb.Height + 2 * waypointRadius;
            var transformGroup = new TransformGroup();
            transformGroup.Children.Add(
                new TranslateTransform(-bb.Left + waypointRadius, 
                    -bb.Top + waypointRadius));
            transformGroup.Children.Add(
                new ScaleTransform(
                    1.0 / width * this.ActualWidth, 
                    1.0 / height * this.ActualHeight));

            this.Waypoints.RenderTransform = transformGroup;
        }
    }
}
